﻿module Sustainalytics.Accounts {
    export class AccountsService {
        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: INotificationFactory;
        private staticObjects: IStaticObjects;
        private $location: ng.ILocationService;

        constructor($q: ng.IQService, $http: ng.IHttpService, $location: ng.ILocationService, notificationFactory: INotificationFactory, staticObjects: IStaticObjects, $upload: any) {
            this.$q = $q;
            this.$http = $http;
            this.notificationFactory = notificationFactory;
            this.staticObjects = staticObjects;
            this.$location = $location;
        }

        private accountsUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/accountsserv/api/v1/accounts/";
        }

        public accountsGetCount(statusString : string, nameString : string): ng.IPromise<number> {

            var ctx = this;

            var def = ctx.$q.defer();

            var promise = ctx.$http.get(ctx.accountsUrl(), { params: $.extend({ getcount: true }, {status: statusString, name: nameString }) });
            promise
                .success(data => {
                    def.resolve(parseInt(<string>data));
                })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public accountsGetLastCrmSync(): ng.IPromise<string>{
            var ctx = this;

            var def = ctx.$q.defer();

            var promise = ctx.$http.get(ctx.accountsUrl(), { params: { returnLastCrmSyncDate: true } });
            promise
                .success(data => {
                    def.resolve(data);
                })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public accountsGetList(gridOpt: Common.HttpGetOptionsDto, status: string, name: string): ng.IPromise<AccountsDto[]> {

            var ctx = this;

            var def = this.$q.defer();

            var promise = this.$http.get(ctx.accountsUrl(), { params: $.extend({ status: status, name: name}, gridOpt) });
            promise
                .success(data => { def.resolve(data); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public accountGet(id: string): ng.IPromise<AccountsDto> {
            var ctx = this;

            var def = this.$q.defer();

            var promise = this.$http.get(ctx.accountsUrl() + id, {});
            promise
                .success(data=> { def.resolve(data); })
                .error(err=> { ctx.showError(err); def.reject(); });

            return def.promise;
        }

      
        // show error message for a given error code
        private showError(err) {
            var ctx = this;
            if (err && err['code'])
                ctx.notificationFactory.error('', ctx.staticObjects.getMessageByInternalCode(err['code']));
        }

    }

    angular.module('Sustainalytics.Accounts')
        .factory('AccountsService',
        ($q: ng.IQService, $http: ng.IHttpService, $location: ng.ILocationService, notificationFactory: INotificationFactory, staticObjects: IStaticObjects, $upload: any) => {
            return new AccountsService($q, $http, $location, notificationFactory, staticObjects, $upload);
        }
    );
}

        



 